package com.example.wyh.intelcheckbash;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.wyh.intelcheckbash.common.SysExitUtil;
import com.example.wyh.intelcheckbash.task.UploadCheckedTaskActivity;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by WYH on 17/10/31.
 */

public class UpdatePasswordActivity extends AppCompatActivity {
    @Override
    public  void  onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.update_password_activity);
        SysExitUtil.activityList.add(UpdatePasswordActivity.this);
        initActionBar();

        //更新版本号
        TextView update_password_version_name = (TextView)findViewById(R.id.update_password_version_name);
        PackageManager packageManager = getPackageManager();
        try {
            PackageInfo info = packageManager.getPackageInfo(getPackageName(), 0);
            String appVersionName = info.versionName; // 版本名
            update_password_version_name.setText("颐达科技 "+appVersionName);
//            Log.e("currentVersionCode,appVersionName",currentVersionCode + " " + appVersionName);
        } catch (PackageManager.NameNotFoundException e) {
            // TODO Auto-generated catch blockd
            e.printStackTrace();
        }


        Button confirm_fix_password = (Button)findViewById(R.id.confirm_fix_password);
        confirm_fix_password.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                EditText password = (EditText)findViewById(R.id.change_password_et);
                String password_str = password.getText().toString();
                EditText repassword = (EditText)findViewById(R.id.change_repassword_et);
                String repassword_str = repassword.getText().toString();
                EditText confirm_password = (EditText)findViewById(R.id.confirm_password);
                String confirm_password_str = confirm_password.getText().toString();
                if (confirm_password_str.equals(repassword_str) == true)
                {
                    JSONObject user = LocalStorage.getObject(UpdatePasswordActivity.this, "User");
                    String username = getValueByKey(user, "userName");
                    HttpUtil.requestNetForUpdataPassword(username, password_str, repassword_str, UpdatePasswordActivity.this);
                }
                else
                {
                    Toast.makeText(UpdatePasswordActivity.this,"确认密码与新密码不一致，请重新输入",Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    public  void  initActionBar(){
        android.support.v7.app.ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setTitle("");
            actionBar.setDisplayShowHomeEnabled(false);
            actionBar.setDisplayShowCustomEnabled(true);
            LayoutInflater inflator = (LayoutInflater) this
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View v = inflator.inflate(R.layout.actionbar_custom,new LinearLayout(UpdatePasswordActivity.this),false);
            android.support.v7.app.ActionBar.LayoutParams layout = new android.support.v7.app.ActionBar.LayoutParams(
                    android.support.v7.app.ActionBar.LayoutParams.MATCH_PARENT, android.support.v7.app.ActionBar.LayoutParams.MATCH_PARENT);
            actionBar.setCustomView(v, layout);
            Toolbar parent = (Toolbar) v.getParent();
            parent.setContentInsetsAbsolute(0, 0);
        }

        TextView actionBarTitle = (TextView) findViewById(R.id.action_bar_title);
        actionBarTitle.setText("修改密码");

        ImageView title_logo = (ImageView)findViewById(R.id.title_logo_id);
        title_logo.setImageResource(R.drawable.set_title);

        RelativeLayout bar_back_btn =  (RelativeLayout) findViewById(R.id.bar_back_btn);
        bar_back_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                UpdatePasswordActivity.this.finish();
            }
        });


    }

    public  String getValueByKey(JSONObject jsonObject, String key ){
        String value ;
        try {
            value = (String) jsonObject.get(key);
        } catch (JSONException e) {
            value = "";
            e.printStackTrace();
        }
        return value;
    }

    public void updatePasswordCallback(){
        Handler mainHander = new Handler(Looper.getMainLooper());
        mainHander.post(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(UpdatePasswordActivity.this,"修改密码成功",Toast.LENGTH_SHORT).show();
            }
        });

    }
}
